﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TiposComuns.Objectos;
using Servidor.WebServices;
using Servidor.Objectos.Pesquisa;

namespace Servidor.Objectos.RemoteActions
{
    [Serializable]
    public class RemoteAddSearchedAction : RemoteAction
    {
        private Person searched;
        private byte[] key;

        public RemoteAddSearchedAction() { }

        public RemoteAddSearchedAction(int id, byte []key, Person searched) : base(id)
        {
            this.searched = searched;
            this.key = key;
        }

        public Person Searched
        {
            get { return searched; }
            set { searched = value; }
        }

        public byte[] Key
        {
            get { return key; }
            set { key = value; }
        }

        public override bool Execute()
        {
            Proxy proxy = Proxy.GetInstance();
            KClosest closest = proxy.GetClosest(searched);
            Peer []nodes = new Peer[closest.Nodes.Lenght];
            int i, size;
            do
            {
                for (i = 0, size = closest.Nodes.Lenght; i < size; i++)
                {
                    Person person = closest.Nodes.Array[i];
                    Peer peer = proxy.GetPeer(person);
                    nodes[i] = peer;

                    if (peer != null && peer.HashSearched(this.key))
                    {
                        KeyValuePair<SearchResult, bool> sr = peer.StoreSearched(this.key, this.searched);
                        if (sr.Key != null)
                        {
                            if (sr.Value)
                            {
                                for (int j = 0; j < size; j++)
                                {
                                    if (j < i)
                                    {
                                        nodes[j].Store(this.key,sr.Key);
                                    }
                                    else if (j > i)
                                    {
                                        proxy.GetPeer(closest.Nodes.Array[j]).Store(this.key,sr.Key);
                                    }
                                }

                                return true;
                            }
                            else
                            {
                                for (int j = 0; j < size; j++)
                                {
                                    if (j < i)
                                    {
                                        nodes[j].Store(this.key,sr.Key);
                                    }
                                    else if (j > i)
                                    {
                                        proxy.GetPeer(closest.Nodes.Array[j]).Store(this.key,sr.Key);;
                                    }
                                }
                                closest = sr.Key.Nexts;
                                break;
                            }
                        }
                    }
                }
                if (i==size)
                {
                    SearchResult sr = new SearchResult(key);
                    sr.Add(this.searched);

                    for (i = 0; i < size; i++)
                    {
                        if (nodes[i] != null)
                        {
                            try
                            {
                                nodes[i].Store(this.key, sr);
                            }
                            catch (Exception e)
                            {

                            }
                        }
                    }

                    return true;
                }

            } while (closest != null);

            return false;
        }
    }
}
